package com.wang.ymcc.mq;

import com.alibaba.fastjson.JSON;
import com.wang.lwm.constents.MQConstants;
import com.wang.lwm.result.JSONResult;
import com.wang.ymcc.domain.MediaFile;
import com.wang.ymcc.service.IMediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = MQConstants.MQ_MEDIA_TOPIC,
        selectorExpression=MQConstants.MQ_MEDIA_TAGS	//tags
        ,consumerGroup = "service-consumer"
        ,messageModel = MessageModel.CLUSTERING ) //消息的消费模式 CLUSTERING：集群    BROADCASTING：广播
@Slf4j
public class MediaMQConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        //把数据取出来进行消费
        byte[] body = messageExt.getBody();//原始消息
        //转化成字符串
        String mediaStr = new String(body);
        //转换为MediaFile
        MediaFile mediaFile = JSON.parseObject(mediaStr, MediaFile.class);
        //切片推流
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        log.info("视频推送 {}" ,jsonResult.isSuccess());
    }
}
